package com.xiaoju.manhattan.optimus.util;

import org.apache.commons.codec.binary.Base64;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;


public class OMGUtil
{
    private static final Charset CHARSET = StandardCharsets.UTF_8;
    
    private static int MX(int sum, int y, int z, int p, int e, int[] k)
    {
        return (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
    }
    
    
    private static final byte[] encrypt(byte[] data, byte[] key)
    {
        if (data.length == 0) {
            return data;
        }
        return toByteArray(encrypt(toIntArray(data, true), toIntArray(fixKey(key), false)), false);
    }
    
    public static final String encrypt(String data)
    {
        if (data == null) {
            return null;
        }
        byte[] bytes = encrypt(data.getBytes(CHARSET), getKey());
        return encodeWithBase64(bytes);
    }
    
    private static final byte[] decrypt(byte[] data, byte[] key)
    {
        if (data.length == 0) {
            return data;
        }
        return toByteArray(decrypt(toIntArray(data, false), toIntArray(fixKey(key), false)), true);
    }
    
    public static final String decrypt(String data)
    {
        if (data == null) {
            return null;
        }
        byte[] bytes = decrypt(decodeWithBase64(data), getKey());
        return new String(bytes, CHARSET);
    }
    
    private static int[] encrypt(int[] v, int[] k)
    {
        int n = v.length - 1;
        
        if (n < 1) {
            return v;
        }
        int p, q = 6 + 52 / (n + 1);
        int z = v[n], y, sum = 0, e;
        
        while (q-- > 0) {
            sum = sum + getDelta();
            e = sum >>> 2 & 3;
            for (p = 0; p < n; p++) {
                y = v[p + 1];
                z = v[p] += MX(sum, y, z, p, e, k);
            }
            y = v[0];
            z = v[n] += MX(sum, y, z, p, e, k);
        }
        return v;
    }
    
    private static int[] decrypt(int[] v, int[] k)
    {
        int n = v.length - 1;
        
        if (n < 1) {
            return v;
        }
        int p, q = 6 + 52 / (n + 1);
        int z, y = v[0], sum = q * getDelta(), e;
        
        while (sum != 0) {
            e = sum >>> 2 & 3;
            for (p = n; p > 0; p--) {
                z = v[p - 1];
                y = v[p] -= MX(sum, y, z, p, e, k);
            }
            z = v[n];
            y = v[0] -= MX(sum, y, z, p, e, k);
            sum = sum - getDelta();
        }
        return v;
    }
    
    private static byte[] fixKey(byte[] key)
    {
        if (key.length == 16) { return key; }
        byte[] fixedkey = new byte[16];
        if (key.length < 16) {
            System.arraycopy(key, 0, fixedkey, 0, key.length);
        } else {
            System.arraycopy(key, 0, fixedkey, 0, 16);
        }
        return fixedkey;
    }
    
    private static int[] toIntArray(byte[] data, boolean includeLength)
    {
        int n = (((data.length & 3) == 0) ? (data.length >>> 2) : ((data.length >>> 2) + 1));
        int[] result;
        
        if (includeLength) {
            result = new int[n + 1];
            result[n] = data.length;
        } else {
            result = new int[n];
        }
        n = data.length;
        for (int i = 0; i < n; ++i) {
            result[i >>> 2] |= (0x000000ff & data[i]) << ((i & 3) << 3);
        }
        return result;
    }
    
    private static byte[] toByteArray(int[] data, boolean includeLength)
    {
        int n = data.length << 2;
        
        if (includeLength) {
            int m = data[data.length - 1];
            n -= 4;
            if ((m < n - 3) || (m > n)) {
                return null;
            }
            n = m;
        }
        byte[] result = new byte[n];
        
        for (int i = 0; i < n; ++i) {
            result[i] = (byte) (data[i >>> 2] >>> ((i & 3) << 3));
        }
        return result;
    }
    
    private static String encodeWithBase64(byte[] b)
    {
        return new String(Base64.encodeBase64(b));
    }
    
    private static byte[] decodeWithBase64(String s)
    {
        return Base64.decodeBase64(s.getBytes());
    }
    
    private static final int getDelta()
    {
        return 0x654AB9E0 ^ 0x85A67F3B - 0x135182AE + 0x001623C;
    }
    
    private final static byte[] getKey()
    {
        byte[] a = new byte[] {0x6d, 0x6e, 0x68, 0x5e, 0x75, 0x5e, 0x6d};
        byte[] b = new byte[] {0x61, 0x5e, 0x61, 0x64, 0x6e, 0x6f, 0x67};
        byte[] bytes = new byte[a.length + b.length];
        for (int i = 0; i < a.length; i++) {
            bytes[i * 2] = a[i];
            bytes[i * 2 + 1] = b[i];
        }
        return bytes;
    }
    
    public static void main(String[] args)
    {
        String s = "对于要克隆的对象，对于其基本数据类型的属性，复制一份给新产生的对象，对于非基本数据类型的属性，仅仅复制一份引用给新产生的对象，即新产生的对象和原始对象中的非基本数据类型的属性都指向的是同一个对象\n" +
                   "1、实现java.lang.Cloneable接口\n" +
                   "要clone的类为什么还要实现Cloneable接口呢？Cloneable接口是一个标识接口，不包含任何方法的！这个标识仅仅是针对Object类中clone()" +
                   "方法的，如果clone类没有实现Cloneable接口，并调用了Object的 clone()方法（也就是调用了super.Clone()方法），那么Object的clone()方法就会抛出 " +
                   "CloneNotSupportedException异常。";
        for (String ss : s.split("，")) {
            String es = encrypt(ss);
            System.out.println(es);
            String ds = decrypt(es);
            System.out.println(ds);
        }
    }
}
